﻿using EM.FrameWork.Paging;
using EM.OA.BLL;
using EM.OA.DataModel.DBModel;
using EM.OA.Portal.Models;
using EM.OA.WebCommon.Base;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace EM.OA.Portal.Controllers
{
    public class ProjectController : BaseAuthController
    {
        public ActionResult Index()
        {
            ViewData["refID"] = Guid.NewGuid().ToString();
            return View();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="rows">每页条数</param>
        /// <returns></returns>
        public JsonResult GetList(string page, string rows)
        {
            ViewData["refID"] = Guid.NewGuid().ToString();
            List<object> result = new List<object>();
            ProjectBLL bll = new ProjectBLL();
            string typeType = GetParam("treeType");
            string projectName = GetParam("projectName");
            PagedList<EMOA_Project> list = bll.GetPagedList(Convert.ToInt32(page), Convert.ToInt32(rows),
                p =>
                {
                    bool tmp = true;
                    if (typeType == "1")//负责的项目
                    {
                        tmp = p.ManageUserID != null && p.ManageUserID == CurrentUser.UserID;
                    }
                    else if (typeType == "2")//参与的项目
                    {
                        tmp = p.JoinUserIDs.IsNotEmpty() && p.JoinUserIDs.Contains(CurrentUser.UserID.ToString());
                    }
                    return tmp && (projectName.IsNullOrEmpty() || p.ProjectName == projectName);
                });
            list.ForEach(p =>
            {
                result.Add(new ProjectModels().ToModel<EMOA_Project>(p));
            });

            return ToJson(result, list.TotalItemCount);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(ProjectModels model)
        {
            try
            {
                //if (ModelState.IsValid)
                //{
                ProjectBLL bll = new ProjectBLL();
                EMOA_Project entity = model.ToEntity<EMOA_Project>();
                entity.ProjectID = Guid.Parse(GetParam("refID"));
                entity.CreateBy = CurrentUser.UserID;
                entity.CreateTime = DateTime.Now;
                if (bll.Add(entity))
                {
                    return Content("OK");
                }
                else
                {
                    return Content("失败");
                }
                //}
                //else
                //{
                //    return Content("验证失败");
                //}
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(ProjectModels model)
        {
            try
            {
                //if (ModelState.IsValid)
                //{
                ProjectBLL bll = new ProjectBLL();
                EMOA_Project entity = bll.GetEntity(model.ProjectID);
                entity = model.ToEntity<EMOA_Project>(entity);
                entity.UpdateBy = CurrentUser.UserID;
                entity.UpdateTime = DateTime.Now;
                if (bll.Update(entity))
                    return Content("OK");
                else
                    return Content("失败");
                //}
                //else
                //{
                //    return Content("验证失败");
                //}
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }


        [HttpPost]
        public ActionResult Delete(string id)
        {
            try
            {
                ProjectBLL bll = new ProjectBLL();
                if (bll.Delete(Guid.Parse(id)))
                    return Content("OK");
                else
                    return Content("失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }
    }
}
